package com.xc.h05.controller.wyl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xc.h05.entity.Articletype;
import com.xc.h05.entity.Company;
import com.xc.h05.entity.Coursepurchase;
import com.xc.h05.entity.User;
import com.xc.h05.service.wyl.CompanyService;
import com.xc.h05.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    private CompanyService companyService;

    @DeleteMapping("/delete")
    public R deleteByPrimaryKey(Long id) {
        int result = companyService.deleteByPrimaryKey(id);
        if (result > 0) {
            return R.ok("删除成功");
        } else {
            return R.fail("删除失败");
        }
    }

    @PostMapping("add")
    public R insert(@RequestBody Company company) {
        int result = companyService.insert(company);
        if (result > 0) {
            return R.ok("插入成功");
        } else {
            return R.fail("插入失败");
        }
    }

    @PostMapping("/selective")
    public R insertSelective(@RequestBody Company company) {
        int result = companyService.insertSelective(company);
        if (result > 0) {
            return R.ok("有选择地插入成功");
        } else {
            return R.fail("有选择地插入失败");
        }
    }

    @GetMapping("/{id}")
    public R selectByPrimaryKey(@PathVariable Long id) {
        Company company = companyService.selectByPrimaryKey(id);
        if (company != null) {
            return R.ok(company);
        } else {
            return R.fail("未查询到对应课程购买记录");
        }
    }

    @PutMapping("/selective")
    public R updateByPrimaryKeySelective(@RequestBody Company company) {
        int result = companyService.updateByPrimaryKeySelective(company);
        if (result > 0) {
            return R.ok("有选择地更新成功");
        } else {
            return R.fail("有选择地更新失败");
        }
    }

    @PutMapping
    public R updateByPrimaryKey(@RequestBody Company company) {
        int result = companyService.updateByPrimaryKey(company);
        if (result > 0) {
            return R.ok("更新成功");
        } else {
            return R.fail("更新失败");
        }
    }

    @GetMapping("findAll")
    public R findAll( Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Company> companyList = companyService.findAll();
        PageInfo< Company> pageInfo = new PageInfo<>(companyList);
        return R.ok(pageInfo);
    }
}
